using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace Microsoft.Expression.Drawing.Core
{
	internal static class PathFigureHelper
	{
		internal static void FlattenFigure(PathFigure figure, IList<Point> points, double tolerance, bool removeRepeat)
		{
			if (figure == null)
			{
				throw new ArgumentNullException("figure");
			}
			if (points == null)
			{
				throw new ArgumentNullException("points");
			}
			if (tolerance < 0.0)
			{
				throw new ArgumentOutOfRangeException("tolerance");
			}
			IList<Point> list = removeRepeat ? new List<Point>() : points;
			list.Add(figure.get_StartPoint());
			using (IEnumerator<PathSegmentData> enumerator = figure.AllSegments().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					PathSegmentData current = enumerator.get_Current();
					current.PathSegment.FlattenSegment(list, current.StartPoint, tolerance);
				}
			}
			if (figure.get_IsClosed())
			{
				list.Add(figure.get_StartPoint());
			}
			if (removeRepeat && list.get_Count() > 0)
			{
				points.Add(list.get_Item(0));
				for (int i = 1; i < list.get_Count(); i++)
				{
					double value = GeometryHelper.SquaredDistance(points.Last<Point>(), list.get_Item(i));
					if (!MathHelper.IsVerySmall(value))
					{
						points.Add(list.get_Item(i));
					}
				}
			}
		}
		public static IEnumerable<PathSegmentData> AllSegments(this PathFigure figure)
		{
			PathFigureHelper.<AllSegments>d__0 <AllSegments>d__ = new PathFigureHelper.<AllSegments>d__0(-2);
			<AllSegments>d__.<>3__figure = figure;
			return <AllSegments>d__;
		}
		internal static bool SyncPolylineFigure(PathFigure figure, IList<Point> points, bool isClosed, bool isFilled = true)
		{
			if (figure == null)
			{
				throw new ArgumentNullException("figure");
			}
			bool flag = false;
			if (points != null && points.get_Count() != 0)
			{
				flag = (flag | figure.SetIfDifferent(PathFigure.StartPointProperty, points.get_Item(0)) | figure.get_Segments().EnsureListCount(1, () => new PolyLineSegment()) | PathSegmentHelper.SyncPolylineSegment(figure.get_Segments(), 0, points, 1, points.get_Count() - 1));
			}
			else
			{
				flag = (flag | figure.ClearIfSet(PathFigure.StartPointProperty) | figure.get_Segments().EnsureListCount(0, null));
			}
			return flag | figure.SetIfDifferent(PathFigure.IsClosedProperty, isClosed) | figure.SetIfDifferent(PathFigure.IsFilledProperty, isFilled);
		}
		internal static bool SyncEllipseFigure(PathFigure figure, Rect bounds, SweepDirection sweepDirection, bool isFilled = true)
		{
			bool flag = false;
			Point[] array = new Point[2];
			Size size = new Size(bounds.get_Width() / 2.0, bounds.get_Height() / 2.0);
			Point point = bounds.Center();
			if (size.get_Width() > size.get_Height())
			{
				array[0] = new Point(bounds.get_Left(), point.get_Y());
				array[1] = new Point(bounds.get_Right(), point.get_Y());
			}
			else
			{
				array[0] = new Point(point.get_X(), bounds.get_Top());
				array[1] = new Point(point.get_X(), bounds.get_Bottom());
			}
			ArcSegment dependencyObject;
			return flag | figure.SetIfDifferent(PathFigure.IsClosedProperty, true) | figure.SetIfDifferent(PathFigure.IsFilledProperty, isFilled) | figure.SetIfDifferent(PathFigure.StartPointProperty, array[0]) | figure.get_Segments().EnsureListCount(2, () => new ArcSegment()) | GeometryHelper.EnsureSegmentType<ArcSegment>(out dependencyObject, figure.get_Segments(), 0, () => new ArcSegment()) | dependencyObject.SetIfDifferent(ArcSegment.PointProperty, array[1]) | dependencyObject.SetIfDifferent(ArcSegment.SizeProperty, size) | dependencyObject.SetIfDifferent(ArcSegment.IsLargeArcProperty, false) | dependencyObject.SetIfDifferent(ArcSegment.SweepDirectionProperty, sweepDirection) | GeometryHelper.EnsureSegmentType<ArcSegment>(out dependencyObject, figure.get_Segments(), 1, () => new ArcSegment()) | dependencyObject.SetIfDifferent(ArcSegment.PointProperty, array[0]) | dependencyObject.SetIfDifferent(ArcSegment.SizeProperty, size) | dependencyObject.SetIfDifferent(ArcSegment.IsLargeArcProperty, false) | dependencyObject.SetIfDifferent(ArcSegment.SweepDirectionProperty, sweepDirection);
		}
	}
}
